
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <script type='text/javascript' src="sysJavascript.js"></script>
    </head>
    <body>

        <form action= ""  method="post" enctype="multipart/form-data"  onsubmit="return validate(this)" >
            <input type="file" name="paper" accept="pdfs" id="paper" />  <br/>
            <input type="submit" name="resubmit" value="Resubmit" /> 
            <br/>
        </form>

            <?php
// Check if a file has been resubmitted
            if (isset($_FILES['paper']))
            {
// Make sure the file was sent without errors
                if ($_FILES['paper']['error'] == 0)
                {
                    //Open connection with database
                    $conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
                    mysql_select_db("mydb") or die(mysql_error());
// Make sure an ID was passed
                    if (isset($_GET['paper_id']))
                    {
// Get the ID
                        $paperid = intval($_GET['paper_id']);
                    }
       //check if the size of the file exceeds 1 MB the maximum size then print a message for the user
          if($_FILES['paper']['size']> 1000000) {
             echo 'The size of the file exceeds the maximum size.Please choose a file of size less than 1 MB';
                 }
              else {
 //check if the file type is not pdf so print a message for the user since the allowed type is pdf only
               if($_FILES['paper']['type'] != 'application/pdf') {
                   echo 'Please only choose a pdf file, other types are not allowed to be submitted';
                            }
                //if its type is pdf so continue submitting
                      else {
                          
                    //if the user click the submit button 
                    if ($_SERVER['REQUEST_METHOD'] == 'POST')
                    {
                        //select the version of the paper selected to resubmit it and fetch it.
                        $getVersionID = "SELECT `version_id` FROM `version` WHERE `paper_id`= $paperid  ORDER BY version_id DESC";
                        $getversionid = mysql_query($getVersionID) or die(mysql_error());
                        $versionID = mysql_fetch_assoc($getversionid);
                        //get the new path of the paper
                        $target_path = "./paper/" . $_FILES['paper']['name'];
                        move_uploaded_file($_FILES['paper']['tmp_name'], $target_path);
                       
                        // add the new version of the paper with its path
                        $AddNewVersion =   mysql_query("INSERT INTO `version` (`paper_id`,`path`)
                     VALUES ($paperid,'{$target_path}')") or die(mysql_error());
                        //take the autoincremented version id
                        include ("statics.php");
                        // Execute the query
                        $last_autoincrement_versionid = mysql_insert_id();
                        exec("pdftohtml -c -noframes " . escapeshellcmd($target_path));
                        iReformat($last_autoincrement_versionid);
                        // Check if it was successfull
                        if ($AddNewVersion)
                        { // if the query run successfully
                            echo 'Success! Your file was successfully resubmitted!';
                        }
                        else
                        {
                            echo 'Error! Failed to insert the file';
                        }


// Close the mysql connection
                        mysql_close($conn);
                    }}
                }}
                else
                {
                    echo 'There is an error while inserting the file!';
                
            }}
            ?>



    </body>


</html>





